<?php
class PFT_server {
	public $err1="101"; //无授权，拒绝连接
	public $err2="102"; //传输数据类型出错
	public $err3="103"; //传输数据格式出错
	public $err4="104"; //数据无法传输
	public $err5="105"; //数据为空或重叠
	public $err6="106"; // 数据服务出错，无法提交订单
	public $err7="107"; // 返回数据非XML格式
	public $err8="108"; // 网络中断
	public $err9="109"; // 重复查询
	public $err10="110"; // 重复提交
	public $err11="111"; // 数据含有非法字符
	public $err12="112"; // 手机格式错误
	public $err13="113"; // 错误的联票方式
	public $err14="114"; // 编码出错，无法继续
	public $err15="115"; // 网站服务出错，请联系技术开发人员
	public $err16="116"; // 短信重发次数过多
	public $err17="117"; // 在线支付未支付成功无法发短信
	public $err18="118"; // 终端信息出错，请联系技术人员
	public $err19="119"; // 订单状态出错，请联系技术人员
	public $err20="120"; // 订单号已超出系统承载，无法生成
	public $err21="121"; // 状态参数出错
	public $err22="122"; // 资金账户余额不足  
	public $err23="123"; // 未知的订单号
	public $err24="124"; // 权限不够

	public $err30="130"; // 邮件格式错误
	public $err31="131"; // 手机格式错误
	public $err32="132"; // 短信插入失败
	public $err33="133"; // 时间未到
	public $err34="134"; // 时间超过

	public $err41="141"; // 无修改返回
	public $err42="142"; // 存在时间交集
	public $err43="143"; // 未按照预期更新数据
	public $err44="144"; // 参加优惠计划的订单不支持修改订单
	public $err45="145"; // 修改订单内容不能全部为空
	
	public $err1061=1061; //无此日期的价格
	public $err1062=1062; //库存已售罄
	public $err1063=1063; //总库存已售罄
	public $err1064=1064; //错误的分销差价设置
	public $err1065=1065; //无此商品的分销价格
	public $err1066=1066; //商品票类数据出错
	public $err1067=1067; //商品数据出错
	public $err1068=1068; //商品需提前预定
	public $err1069=1069; //已超过当日购买的时间
	public $err1070=1070; //支付方式错误
	public $err1071=1071; //游玩时间必填
	public $err1072=1072; //取票人姓名、手机不能为空
	public $err1073=1073; //错误的供应链
	public $err1074=1074; //短信发送失败
	
	public $t1="uu_land";
    public $t2="uu_jq_ticket";
	public $t3="uu_ticket_pprice";
	public $t4="uu_jq_order";
	public $t5="uu_area";
	public $t6="uu_order_trace";
	public $t7="uu_ss_order";
	
    //private $auth=false;

//验证身份
	public function authrize($ac, $pw){
		$ac=urlencode($ac);
		$pw=urlencode($pw);
	
	    $str="select id,vtype from dataex where account=\"".$ac."\" and pwd=\"".$pw."\" and vtype=0 limit 1";
        $GLOBALS['le']->query($str);
	    if ($GLOBALS['le']->fetch_assoc()) {
			$this->vtype=$GLOBALS['le']->f('vtype');
			return true;
		}else{
			return false;
		}
	}
//景区列表
	public function Get_ScenicSpot_List($ac, $pw, $n=NULL, $m=NULL) {
	
		if ($this->authrize($ac, $pw)){
		$str="select id from pft_member where dcode='$ac'";
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$fxs_id=$GLOBALS['le']->f('id');
		
		if ($fxs_id){
			$ap=array();
			$str="select tid from uu_priceset where pid='$fxs_id'";	
			$GLOBALS['le']->query($str);
			while ($GLOBALS['le']->fetch_assoc()) {
			$ap[]=$GLOBALS['le']->f('tid');
			}
			
						
			$apstr=implode(',',$ap);
			$aland=array();		
			$str="select landid from uu_jq_ticket where pid in ($apstr) or apply_did=$fxs_id group by landid";	
			$GLOBALS['le']->query($str);
			while ($GLOBALS['le']->fetch_assoc()) {
			$aland[]=$GLOBALS['le']->f('landid');
			}
			
			$alandstr=implode(',',$aland);
			
		}
		
		if (!$alandstr) return errXML($this->err5);
		
		$a1=array();
		$str="select id,title,addtime,imgpath,area from ".$this->t1." where status='1' and id in ($alandstr) order by px desc";
		if ($n) $str.=" limit ".$n;
		if ($m && $n) $str.=",$m";
        $GLOBALS['le']->query($str);
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a1[]=$row;
		}
		//return $str;
		  if (count($a1)<1) {
		     return errXML($this->err5);
		   }else{
			 foreach ($a1 as $key=>$val){  
			  $kword=$val[area];
			  $akword=explode("|",$kword);
			  $apword=array();
			  
			  $str="select area_name from ".$this->t5." where area_id='".$akword[0]."' or area_id='".$akword[1]."' or area_id='".$akword[2]."'";
			  $GLOBALS['le']->query($str);
		      while ($GLOBALS['le']->fetch_assoc()) {
                 $apword[]=$GLOBALS['le']->f('area_name');
		      }
			  //return implode('|',$apword);
			  $a1[$key]['area']=implode('|',$apword);			  
			 }
		     return toXML($a1);
		   }
		
		}else{
		return errXML($this->err1);
		}

    }

//景区详细
	public function Get_ScenicSpot_Info($ac, $pw, $n) {

		if ($this->authrize($ac, $pw)){
		$a2=array();
		$str="select id,salerid,title,tel,fax,jtype,opentime,runtime,area,address,jtzn,jdjj,bhjq,yllx,jqts,zbss,zbms,tctj,qt,imgpath from ".$this->t1." where id='$n'";

        $GLOBALS['le']->query($str);
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a2[]=$row;
		}
		//return $str;
		if (count($a2)<1) return errXML($this->err5);
		else return toXML($a2);
		}else{
		return errXML($this->err1);	
		}

    }

//门票查询
	public function Get_Ticket_List($ac, $pw, $n=NULL, $m=NULL) {

		if ($this->authrize($ac, $pw)){
		if (!$n && !$m) return errXML($this->err5);	
		$a3=array();
		$str="select f.lid,t.id,t.title,t.pid,t.delaydays,t.notes,t.ddays,t.reb,f.dhour,f.startplace,f.endplace,f.tourist_info,f.ass_station,f.series_model from ".$this->t2." as t left join uu_land_f as f on t.id=f.tid where 1=1";
		if ($m) $str.=" and t.id='".(int)$m."'";
		else $str.=" and t.landid='$n'";
		$str.=" order by t.px desc";
        $GLOBALS['le']->query($str);
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a3[]=$row;
		}
		//return $str;
		if (count($a3)<1) return errXML($this->err5);		
		else return toXML($a3);
		}else{
		return errXML($this->err1);	
		}

    }

//[接口]动态价格、库存上限获取 产品ID，日期，模式[1单个价格 2单个最低价]，类型[0分销价 1零售价]，获取当日库存上限返回-1无限
	public function Dynamic_Price_And_Storage($ac, $pw, $pid, $date='', $mode=0, $ptype=0, $get_storage=0) {
		if ($this->authrize($ac, $pw)){
		if (is_numeric($pid)===false) return $this->err3;
		$a5=array();
		$a51=array();
		//取得此分销商的差价
		$str="select id from pft_member where account='$ac' limit 1";
        $GLOBALS['le']->query($str);
		if (!$GLOBALS['le']->fetch_assoc()) return errXML($this->err1064);	
		$d_id=$GLOBALS['le']->f('id');
		
		$str="select dprice from uu_priceset where status=0 and tid='$pid' and pid='$d_id' limit 1";
        $GLOBALS['le']->query($str);
		if (!$GLOBALS['le']->fetch_assoc()) return errXML($this->err1065);	
		$dprice=$GLOBALS['le']->f('dprice');

		
		//取单个价格
		$onday=date('w',strtotime($date));
		if ($mode==1 && $date){
			//特殊日价格
			$q_s_price=($ptype==0)?'s_price':'l_price as s_price';
			$str="select $q_s_price,storage from uu_product_price where start_date<='$date' and end_date>='$date' and pid=$pid and ptype=1 and string_bj(weekdays,$onday)=1 order by id desc limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$s_price=$GLOBALS['le']->f('s_price');
			$storage=$GLOBALS['le']->f('storage');
			if (is_numeric($s_price)) {
				$tprice=$s_price+$dprice;
				$tprice=($ptype==0)?$tprice:$s_price;
				//$tprice=round($tprice/100,2);
				$a51['sprice']=$tprice;
				if ($get_storage==1) $a51['storage']=$storage;
				$a5[]=$a51;
				return toXML($a5); 
			}
			
			$q_s_price=($ptype==0)?'n_price':'l_price as n_price';
			$str="select $q_s_price,storage from uu_product_price where start_date<='$date' and end_date>='$date' and pid=$pid and ptype=0 and string_bj(weekdays,$onday)=1 order by id desc limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$n_price=$GLOBALS['le']->f('n_price');
			$storage=$GLOBALS['le']->f('storage');
			if (is_numeric($n_price)) {
				$tprice=$n_price+$dprice;			
				$tprice=($ptype==0)?$tprice:$n_price;
				//$tprice=round($tprice/100,2);
				$a51['sprice']=$tprice;
				if ($get_storage==1) $a51['storage']=$storage;
				$a5[]=$a51;
				return toXML($a5); 
			}
			
			return errXML($this->err5);
		}elseif($mode==2){
			//单个有效最低价
			$q_s_price=($ptype==0)?'s_price':'l_price';
			$str="select min($q_s_price) as mp from uu_product_price where date_add(end_date,interval 1 day)>now() and pid=$pid and $q_s_price>=0";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$mp=$GLOBALS['le']->f('mp');
			
			$q_s_price=($ptype==0)?'n_price':'l_price';
			$str="select min($q_s_price) as np from uu_product_price where date_add(end_date,interval 1 day)>now() and pid=$pid and $q_s_price>=0";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$np=$GLOBALS['le']->f('np');
			
			$lp=($mp>=$np)?$mp:$np;
			if (is_numeric($lp) && $lp>=0) {
				$tprice=$lp+$dprice;			
				$tprice=($ptype==0)?$tprice:$lp;
				//$tprice=round($tprice/100,2);
				$a51['sprice']=$tprice;
				$a5[]=$a51;
				return toXML($a5); 
			}
			return errXML($this->err5);
		}
		/* else{
			$str="select id,pid,ptype,n_price,s_price,l_price,start_date,end_date,memo,weekdays,storage from uu_product_price where pid=$pid";
			if ($sdate!=='' && $edate!=='') $str.=" and greatest(start_date,'$sdate')<=least(end_date,'$edate')";
			$str.=" order by id desc";
			//$a5[]=array("fprice"=>$fprice);
        	$GLOBALS['le']->query($str);
			while ($row=$GLOBALS['le']->fetch_assoc()){
				$a5[]=$row;
			}			
			return toXML($a5);			
		} */
		return errXML($this->err5);
		}else{
		return errXML($this->err1);	
		}

    }	
	

/*票付通平台订单接口 （外部）
2景区ID,3票类ID,远端订单号,单价(分单位),数量,游玩时间,取票人姓名,取票人手机,联系人手机,是否发送短信1不发送,支付方式（0账户余额支付2使用供应商处金额支付）,下单方式（0正常下单 1手机用户下单 注：手机用户下单可以用取票人手机当作登录号登入票付通系统）,集合地点,团号）
*/
	public function PFT_Order_Submit($ac, $pw, $lid, $tid, $remotenum=0, $tprice, $tnum, $totalmoney=0, $playtime, $ordername, $ordertel, $contactTEL, $smsSend=0, $paymode=0, $ordermode=0, $assembly=0, $series='', $concatID=0, $pCode=0, $m=NULL) {
		if ($this->authrize($ac, $pw)){
		require_once("../conf/pg.le.je");
  		$lee = new pgconn();
		$lee->getConn();
		
		if ($paymode!=0 && $paymode!=2) return errXML($this->err1070);
		if (!is_numeric($lid) || !is_numeric($tid) || !is_numeric($tnum) || !is_numeric($tprice)) return errXML($this->err3);
		if (!$playtime) return errXML($this->err1071);
		if (!$ordername || !$ordertel) return errXML($this->err1072);
		
		$member=(int)$ac;

		//判断是否有此产品
		$str="select id,salerid,title,p_type from ".$this->t1." where id='$lid'";
		$GLOBALS['le']->query($str);
		$ajq=$GLOBALS['le']->fetch_assoc();
		if (!$ajq) return errXML($this->err1067);
		$salerid=$ajq['salerid'];
		
		$str="select title,delaydays,ddays,getaddr,apply_did,storage_open from ".$this->t2." where id='$tid'";
        $GLOBALS['le']->query($str);
		if (!$atk=$GLOBALS['le']->fetch_assoc()) return errXML($this->err1066);
		$aid=$atk['apply_did'];
		if (!$aid) return errXML($this->err1073);
		if ($member==$aid) $paymode==3;
		
		$pid=$atk['pid'];
		$delaydays=$atk['delaydays'];
		$ttitle=$atk['title'];
		$storage_open=$atk['storage_open'];
		
		if ($paymode!=3){
			//判断是否可以购买此供应商下的产品
			$str="select pids from pft_product_sale_list where fid=$member and aid=$aid and status=0 limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$pids=$GLOBALS['le']->f('pids');
			if ($pids!='A'){
				$a_pids=explode(',',$pids);
				if (!in_array($pid,$a_pids)) return errXML($this->err24);
			}
			
			//取得动态供货价
			$ddprice=$this->get_Dynamic_Price_Merge($pid, $playtime, 1, '', '', 0, 1);			
			if ($ddprice==-1) return errXML($this->err1061);
			
			$a_ddprice=explode(',',$ddprice);
			$ddprice=$a_ddprice[0];
			$ddstorage=$a_ddprice[1];
			if ($ddstorage!=-1) {
				//获取实时库存
				$dcstorage=$this->Dynamic_Storage_Check($tid, 1, $playtime, NULL, 1);
				$a_dcstorage=explode('|',$dcstorage);
				$d_cstorage=$a_dcstorage[1];
				$d_cstorage=(int)$d_cstorage+$tnum;
				
				if ($d_cstorage>$ddstorage) return errXML($this->err1062);			
			}
			
			if ($storage!=-1){
				//获取实时总库存
				$t_storage=$this->Dynamic_Storage_Check($tid, 2, NULL, $storage_open, 1);	
				//$t_storage=(int)$t_storage+$tnum;
				if ($t_storage>$storage) return errXML($this->err1063);			
			}
			
			
			//取得此分销商的差价
			$str="select id from pft_member where account='$ac'";
			$GLOBALS['le']->query($str);
			if (!$GLOBALS['le']->fetch_assoc()) return errXML($this->err1064);
			$d_id=$GLOBALS['le']->f('id');
			
			$str="select dprice from uu_priceset where status=0 and tid='$pid' and pid='$d_id' limit 1";
			$GLOBALS['le']->query($str);
			if (!$GLOBALS['le']->fetch_assoc()) return errXML($this->err1065);		
			$dprice=$GLOBALS['le']->f('dprice');
			$tprice=$ddprice+$dprice;
			//$tprice=round($tprice/100,2);
			
			
		}		
		
		if ($atk['ddays']!=0){
			$nowtime=strtotime(date('Y-m-d'));
			$ddays=$atk['ddays'];
			$excepttime=strtotime("+$ddays days",$nowtime);
			$s_playtime=strtotime($playtime);
			
			if ($s_playtime<$excepttime) return errXML($this->err1068);
			if ($s_playtime==$excepttime){
				$str="select dhour from uu_land_f where pid='$pid'";
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$dhour=$GLOBALS['le']->f('dhour');
				$nowhour=date("H:i:s");
				if ($dhour<$nowhour) return errXML($this->err1069);
			}	
		}

		$totalmoney=$tprice*$tnum;
		//$begintime, $endtime
		$begintime=$playtime;
		$end_ptime0=strtotime($playtime);
		$end_ptime1=strtotime("+".$delaydays." day",$end_ptime0);
		$end_ptime=date("Y-m-d",$end_ptime1);
		$end_ptime.=" 23:59:59";
		
		//判断金额是否充足,下单成功后将扣除金额
		if ($paymode==0){
			$str="select amoney from pft_member_money where fid=$member limit 1";
        	$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$amoney=$GLOBALS['le']->f('amoney');
			if ($totalmoney>$amoney) return errXML($this->err22);
		}elseif($paymode==2){
			$str="select basecredit,kmoney from pft_member_credit where fid=$member and aid=$aid limit 1";
        	$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$kmoney=$GLOBALS['le']->f('kmoney');
			$basecredit=$GLOBALS['le']->f('basecredit');
			$Kkmoney=$kmoney+$basecredit;
			if ($totalmoney>$Kkmoney) return errXML($this->err22);
		}

		//订单号生成
		//$str="select max(id) as mid from ".$this->t7." where date_format(ordertime,'%y%m')='".$y."'";
		$str="select max(id) as mid from ".$this->t7;
        $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$orderid=$GLOBALS['le']->f('mid');
		$orderid=$orderid?($orderid+100000):100000;
		$ordern=$orderid;

		//生成凭证码
		$ch1=600000; //标尺1
		$ch2=800000; //标尺2
		$ch3=1000000; //标尺3
		if ($pCode==1){
			$str="select certnum as mid from ".$this->t7." s left join uu_order_addon a on s.ordernum=a.orderid where a.ifpack=1 and s.status<>4 and s.certnum>=".$ch2." and s.certnum<".$ch3;
			$str.=" order by s.id desc limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$mid=$GLOBALS['le']->f('mid');

			if (!$mid) $mid=$ch2;
			$mid++;
			if ($mid>=$ch3) $mid=$ch2;

			//回收机制
			$str="select id,ordernum from ".$this->t7." where status<>4 and certnum='$mid'";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$kill_id=$GLOBALS['le']->f('id');
			$kill_ordernum=$GLOBALS['le']->f('ordernum');
			if ($kill_id){
				$str="update ".$this->t7." set status=4 where id='$kill_id'";
				$GLOBALS['le']->query($str);
				$pstr="update order_info set order_state=7 where id=$kill_ordernum";
				$lee->pgquery($pstr);
			}
		}else{
			if ($pCode>100000){
				$table_T=$this->t7;
				$str="select certnum,salerid from ".$table_T." s left join uu_order_addon a on s.ordernum=a.orderid where a.ifpack=1 and s.ordernum='".$pCode."'";
				$str.=" order by s.id desc limit 1";
				if (!$GLOBALS['le']->query($str)) return errXML($this->err15);
				$GLOBALS['le']->fetch_assoc();
				$s_certnum=$GLOBALS['le']->f('certnum');
				$s_salerid=$GLOBALS['le']->f('salerid');
			}
			$str="select certnum as mid from ".$this->t7." where salerid='".$salerid."' and status<>4 and certnum>=".$ch1." and certnum<".$ch2;
			$str.=" order by id desc limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$mid=$GLOBALS['le']->f('mid');

			if (!$mid) $mid=$ch1;
			$mid++;
			if ($mid>=$ch2) $mid=$ch1;

			//回收机制
			$str="select id,ordernum from ".$this->t7." where status<>4 and certnum='$mid'";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$kill_id=$GLOBALS['le']->f('id');
			$kill_ordernum=$GLOBALS['le']->f('ordernum');
			if ($kill_id){
				$str="update ".$this->t7." set status=4 where id='$kill_id'";
				$GLOBALS['le']->query($str);
				$pstr="update order_info set order_state=7 where id=$kill_ordernum";
				$lee->pgquery($pstr);
			}
		}

		$str="insert ".$this->t7." set member='$member',ordernum='$ordern',certnum='$mid',remotenum='$remotenum',lid='$lid',tid='$tid',aid='$aid',begintime='$begintime',endtime='$endtime',tnum='$tnum',tprice='$tprice',ordername='$ordername',ordertel='$ordertel',contacttel='$contactTEL',ordertime=now(),salerid='$salerid',paymode='$paymode',ordermode='$ordermode',totalmoney=$totalmoney";
		$GLOBALS['le']->query($str);

		//构造凭证号
		if ($pCode>100000){
		$cert=hashstr((string)$s_certnum, $s_salerid);
		}else{
		$cert=hashstr((string)$mid, $salerid);
		}

		//终端处理
		//$pay_mode=1;
		$pay_status=($paymode==1)?2:1;

		$pstr="select id from merch_info where compn_id='".$salerid."'";
		$lee->pgquery($pstr);
		$lee->pgFetchAssoc();
		$merch_id=$lee->f('id');
		if (!$merch_id) return errXML($this->err18);

		$pstr="select branch_id from term_info where id='".$merch_id."'";
		$lee->pgquery($pstr);
		$lee->pgFetchAssoc();
		$branch_id=$lee->f('branch_id');
		if (!$branch_id) return errXML($this->err18);

        $asrc=array();
		$asrc['id']=$ordern;
		$asrc['src_order']=$branch_id;
		$asrc['src_merch']=$merch_id;
		$asrc['dst_merch']=$salerid;
		$asrc['client_name']="'".$ordername."'";
		$asrc['phone_no']="'".$ordertel."'";
		$asrc['order_state']=0;
		$asrc['total_price']=$totalmoney;
		$asrc['pay_mode']=$pay_status;
		$asrc['prepays']=0;
		$asrc['vouch_code']="'".$salerid.$cert."'";
		$asrc['order_time']="now()";
		if ($delaydays==0) $asrc['begin_time']="'".$begintime."'";
		else $asrc['begin_time']="now()";
		$asrc['end_time']="'".$endtime."'";
		$asrc['ins_time']="now()";
		$asrc['upd_time']="now()";
		$asrc['tid']=$tid;
		$asrc['tprice']=$tprice;
		$asrc['tnum']=$tnum;
		$asrc['linkid']=$concatID;
		$asrc['linkto']=$pCode;
		$asrc['pid']=$pid;
		$pstr="insert into order_info (".make_sql($asrc).") values (".make_sql($asrc,2).")";
		$lee->pgquery($pstr);

        $as2=array();
		$as2['order_id']=$ordern;
		$as2['wares_id']=$tid;
		$as2['quantity']=$tnum;
		$as2['wares_state']=0;
		$as2['begin_time']="now()";
		$as2['end_time']="'".$endtime."'";
		$as2['accept_time']="now()";
		$as2['verify_time']="now()";
		$as2['ins_time']="now()";
		$as2['upd_time']="now()";
		$as2['tw_price']=$tprice;
		$as2['pid']=$pid;
		//$as2['pid']=$tprice*100;

		$pstr="insert into order_wares (".make_sql($as2).") values (".make_sql($as2,2).")";
		$lee->pgquery($pstr);

		$pstr="select id from wares_info where pid='".$pid."'";
		$lee->pgquery($pstr);
		$lee->pgFetchAssoc();
		$pg_tid=$lee->f('id');

		$as3=array();
		$as3['id']=$tid;
		$as3['merch_id']=$salerid;
		$as3['wares_type']=1;
		$as3['wares_name']="'".$ttitle."'";
		$as3['wares_desc']="''";
		$as3['wares_unit']="''";
		$as3['wares_price']=$tprice;
		$as3['ins_time']="now()";
		$as3['upd_time']="now()";
        $as3['pid']=$pid;


		if ($pg_tid) {
		$pstr="update wares_info set ";
		foreach ($as3 as $key=>$val) {
		  if ($key!="id"){
			$pstr.=$key."=".$val;
			if ($key!="pid") $pstr.=",";
		  }
		}
		$pstr.=" where pid='$pid'";

		}else{
		$pstr="insert into wares_info (".make_sql($as3).") values (".make_sql($as3,2).")";
		}
		$lee->pgquery($pstr);


		//处理资金账户
		if ($paymode==0){
			$dismoney=$totalmoney;
			$str="update pft_member_money set amoney=amoney-$dismoney where fid=$member";
			$GLOBALS['le']->query($str);
			//资金流向记录
			$dmoney=$dismoney;
			$lmoney=$amoney-$dmoney;
			$str="insert pft_member_journal set fid=$member,dmoney=$dmoney,lmoney=$lmoney,orderid='$ordern',daction=1,rectime=now(),dtype=0,ptype=0";
			$GLOBALS['le']->query($str);			
		}elseif($paymode==2){
		//处理供应商可用余额
			$dscore=$totalmoney;
			$str="update pft_member_credit set kmoney=kmoney-$dscore where fid=$member";
			$GLOBALS['le']->query($str);
			//资金流向记录
			$dmoney=$dscore;
			$lmoney=$kmoney-$dmoney;
			$str="insert pft_member_journal set fid=$member,dmoney=$dmoney,lmoney=$lmoney,aid=$aid,orderid='$ordern',daction=1,rectime=now(),dtype=0,ptype=2";
			$GLOBALS['le']->query($str);	
		}

		//余额清分
		if ($paymode==0){
			//取出几种费用
/* 			$str="select fee_platform,fee_bank,fee_sms from pft_member where id=$aid limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			//$fee_platform=$GLOBALS['le']->f('fee_platform');
			$fee_bank=$GLOBALS['le']->f('fee_bank');
			//$fee_sms=$GLOBALS['le']->f('fee_sms');
			
			$fees=floor($fee_bank*$totalmoney/1000); */
						
			//资金清分到供应商
			//$aid_money=$totalmoney-$fees;
			$aid_money=$totalmoney;
			$str="update pft_member_money set amoney=amoney+$aid_money where fid=$aid";
			$GLOBALS['le']->query($str);
			$str="select amoney from pft_member_money where fid=$aid";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$l_aid_money=$GLOBALS['le']->f('amoney');
						
			$str="insert pft_member_journal set fid=$aid,dmoney=$aid_money,lmoney=$l_aid_money,orderid='$ordern',daction=0,rectime=now(),dtype=0,ptype=0";
			$GLOBALS['le']->query($str);			
			
			
/* 			//资金清分到平台
			$str="update pft_member_money set amoney=amoney+$fees where fid=1";
			$GLOBALS['le']->query($str);
			$str="select amoney from pft_member_money where fid=1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$l_admin_money=$GLOBALS['le']->f('amoney');
						
			$str="insert pft_member_journal set fid=1,dmoney=$fees,lmoney=$l_admin_money,orderid='$ordern',daction=0,rectime=now(),dtype=9,ptype=0";
			$GLOBALS['le']->query($str); */
		
		}
/* 			//供货价
			$f_price=$this->get_Dynamic_Price_Merge($pid, $begintime, 1);
			//零售价
			$u_price=$this->get_Dynamic_Price_Merge($pid, $begintime, 1, '', '', 1); */

		//确认订单状态更新
		$str="select d_status from uu_products where id='".$pid."'";
        $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$d_status=$GLOBALS['le']->f('d_status');
		$d_stat=(!$d_status)?0:1;
		$str="insert into uu_order_addon set orderid='$ordern',dstatus=$d_stat,ifpack=$pCode";
        $GLOBALS['le']->query($str);
		
		//订单集合地点、支付状态等等
		if ($assembly || $series || $ordern) {
			$str="select ass_station from uu_land_f where pid='$pid'";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$ass_station=$GLOBALS['le']->f('ass_station');
			$aass=unserialize($ass_station);
			$assembly_place=$aass[$assembly];

			$str="insert uu_order_fx_details set assembly='$assembly',series='$series',orderid='$ordern',pay_status='$pay_status'";
			$GLOBALS['le']->query($str);
		}
		
		$a5=array();

        if ($smsSend!=1) {
			require_once("../tel/tel.php");
			require_once("../tel/order1.php");
	
			if ($num>0){
			$str="update uu_ss_order set remsg=1 where ordernum='$ordern'";
			$GLOBALS['le']->query($str);
			$a51=array('ordernum'=>$ordern,'remotenum'=>$remotenum);
			$a5[]=$a51;
			return toXML($a5);
			//return $ordern;
			}else{
			$str="update uu_ss_order set smserror='$num' where ordernum='$ordern'";
			$GLOBALS['le']->query($str);
			return errXML($this->err1074);
			}
		}else{
			$a51=array('ordernum'=>$ordern,'remotenum'=>$remotenum,'code'=>$cert);
			$a5[]=$a51;
			return toXML($a5);
		}

		}else{
		return errXML($this->err1);
		}

    }

//（票付通外部）订单统一查询搜索接口（景区编号,分销商ID（逗号分隔）,供应商ID（逗号分隔）,门票ID,景区模糊标题,门票模糊标题,下单时间开始,下单时间结束,预计游玩时间开始,预计游玩时间结束,订单完成时间开始,订单完成时间结束,票付通订单号,商家订单号,取票人姓名,取票人手机,状态（0未使用|1已使用|2已过期|3被取消|4凭证码被替代）,支付状态（1已支付|2未支付）,排序(1下单时间|2游玩时间|3实际使用时间|4商户ID|5景区标题|6取消时间), 升序或降序（0升序|1降序), 记录起始指针, 返回条数, 返回类型（0详细1返回总数2逗号隔开字符串#订单数,票数,总数）, 联系人手机, 支付（为空不做筛选0帐号余额支付1支付宝2使用供应商可用金额支付),产品类型（A景区B线路C酒店F套票）,关联订单(0显示所有订单1不显示关联订单）,套票（默认显示主票、1显示包含子票、2仅包含子票）
	public function Order_Globle_Search($ac, $pw, $sid, $mid, $aid, $tid, $ltitle, $ttitle, $btime1, $etime1, $btime2, $etime2, $btime3, $etime3, $ordernum, $remotenum, $oname, $otel, $status='', $pays='', $orderby=1, $sort=0, $rstart=0, $n=40, $c=0, $contactTEL, $payinfo="", $p_type='', $concat=0, $ifpack=0, $m=NULL) {
		if ($this->authrize($ac, $pw)){
		if ($c!=0 && $c!=1 && $c!=2) return errXML($this->err2);

        $ordernum=trim($ordernum);
		//身份识别0供应商1分销商
		$str="select id,dtype from pft_member where account='$ac'";
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$dtype=$GLOBALS['le']->f('dtype');
		$id=$GLOBALS['le']->f('id');
		
		//分销商只能查询自己的订单
		if ($dtype==0) $aid=$id;
		elseif ($dtype==1) $mid=$id;
		else return errXML($this->err24);	
		
		$str="";

		//查询订单SQL构造

		$str.="select ";

		if ($c==0) $str.="d.account,d.dname,s.ordernum,s.remotenum,s.certnum,s.lid,s.tid,l.title as ltitle,t.title as ttitle,t.getaddr,s.begintime,s.ordertime,s.endtime,s.tnum,s.tprice,s.ordername,s.ordertel,s.status,s.salerid,s.dtime,s.remsg,fd.pay_status as paystatus,s.totalmoney,s.contacttel,s.smserror,s.paymode as pmode,s.ordermode,s.ctime,t.pid,fd.origin,fd.memo,l.p_type,r.dhour,t.status as cstatus,r.mdays,r.mhour,r.startplace,r.endplace,r.rdays,n.dstatus as order_confirm,n.confirm_time,n.confirm_op,fd.series,fd.assembly,s.aid,fd.concat_id,n.ifpack";

		//统计2
		else $str.="s.tnum,s.tprice,s.totalmoney,s.onsale";

		$str.=" from uu_ss_order s left join uu_land l on s.lid=l.id left join uu_jq_ticket t on s.tid=t.id left join pft_member d on s.member=d.id left join uu_land_f r on s.tid=r.tid left join uu_order_fx_details fd on s.ordernum=fd.orderid left join uu_order_addon n on s.ordernum=n.orderid left join uu_order_apply_info a on s.ordernum=a.orderid";
		if ($ordernum) {
		$str.=" where (s.ordernum='".htmlspecialchars($ordernum)."' or fd.concat_id='".htmlspecialchars($ordernum)."') or n.ifpack='".htmlspecialchars($ordernum)."'";
		if ($ifpack==0) $str.=" and (n.ifpack<10000 or n.ifpack is NULL)";
		elseif ($ifpack==2) $str.=" and (n.ifpack<>1 or n.ifpack is NULL)";
		}else{
		$str.=" where 1=1";
		if ($sid) $str.=" and s.salerid in ($sid)";
		if (strpos($mid,',')!==false) $str.=" and d.id in ($mid)";
		elseif ($mid) $str.=" and d.id='$mid'";
		if ($lid) $str.=" and s.lid='$lid'";
		else $str.=" and s.lid>0";
		if ($tid) $str.=" and s.tid='$tid'";
		if ($ltitle) $str.=" and instr(l.title,'".$ltitle."')>0";
		if ($ttitle) $str.=" and instr(t.title,'".$ttitle."')>0";
		if ($btime1) $str.=" and s.ordertime>='$btime1'";
        if ($etime1) $str.=" and s.ordertime<='$etime1'";
		if ($btime2) $str.=" and s.begintime>='$btime2'";
        if ($etime2) $str.=" and s.begintime<='$etime2'";
		if ($btime3) $str.=" and s.dtime>='$btime3'";
        if ($etime3) $str.=" and s.dtime<='$etime3'";
		if ($oname) $str.=" and instr(s.ordername,'".$oname."')>0";
		if ($otel) $str.=" and s.ordertel='".$otel."'";
		if ($contactTEL) $str.=" and s.contacttel='".$contactTEL."'";
		//if ($fromt) $str.=" and d.oid='$fromt'";
		if ($remotenum) $str.=" and s.remotenum='$remotenum'";
		if ($origin) $str.=" and fd.origin='$origin'";
		if ($p_type) $str.=" and l.p_type='$p_type'";
		if (strpos($aid,',')!==false) $str.=" and s.aid in ($aid)";
		elseif ($aid) $str.=" and s.aid='$aid'";
		//if ($aid!=='') $str.=" and s.aid='$aid'";
		if ($concat==1) $str.=" and (fd.concat_id='' or fd.concat_id='main')";
		if ($ifpack==0) $str.=" and (n.ifpack<10000 or n.ifpack is NULL)";
		elseif ($ifpack==2) $str.=" and (n.ifpack<>1 or n.ifpack is NULL)";
		if ($order_confirm!=='') {
			if ($st=$this->stt($order_confirm, "n.dstatus")) {
		    $str.=$st;
			}else{
			return $this->err21;
			}
		}
		if ($status!=='') {
			if ($st=$this->stt($status, "s.status")) {
		    $str.=$st;
			}else{
			return $this->err21;
			}
		}

		if ($pays!=='') {
			if ($st=$this->stt($pays, "fd.pay_status")) {
		    $str.=$st;
			}else{
			return $this->err21;
			}
		}

		if ($payinfo!=='') $str.=" and s.paymode=$payinfo";
		}


		if ($c==0){
		$asc=$sort?"desc":"asc";
		  switch ($orderby) {
			case 1: $str.=" order by ordertime ".$asc;break;
			case 2: $str.=" order by begintime ".$asc;break;
			case 3: $str.=" order by dtime ".$asc;break;
			case 4: $str.=" order by salerid ".$asc;break;
			case 5: $str.=" order by ltitle ".$asc;break;
			case 6: $str.=" order by ctime ".$asc;break;
			default:$str.=" order by ordertime ".$asc;break;
		  }
		$str.=" limit $rstart,$n";
		}

		$a1=array();
		if (!$GLOBALS['le']->query($str)) return $this->err3;
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a1[]=$row;
		}

		if ($c==1) {
			$ca1=count($a1);
			$a2[]=array('count'=>$ca1);
			return toXML($a2);
		}
		if ($c==2) {
			$sum1=count($a1);//订单数
			$sum2=0;//票数
			$sum3=0;//总额
		    foreach ($a1 as $v){
			  $sum2+=$v[tnum];
			  if ($v['onsale']==1) $sum3+=$v[totalmoney];
			  else $sum3+=$v[tprice]*$v[tnum];
			}
			$ca2=$sum1.','.$sum2.','.$sum3;
			$a3[]=array('countstring'=>$ca2);
			return toXML($a3);
		}

		return toXML($a1);

		}else{
		return errXML($this->err1);
		}

    }

//订单短信发送、重发接口 (票付通订单号)
	public function reSend_SMS_Global_PL($ordern, $m=NULL) {
		if ($this->authrize($ac, $pw)){
		$table=$this->t7;
		//$playtime=($ordern>$this->orderCounter)?'playtime as ptime':'begintime as ptime';

		$str="select * from $table where ordernum='$ordern' limit 1";

        $GLOBALS['le']->query($str);
		if (!$GLOBALS['le']->fetch_assoc()) return errXML($this->err5);

		$lid=$GLOBALS['le']->f('lid');
		$tid=$GLOBALS['le']->f('tid');
		$remsg=$GLOBALS['le']->f('remsg');
		$certnum=$GLOBALS['le']->f('certnum');
		$salerid=$GLOBALS['le']->f('salerid');
		$ordertel=$GLOBALS['le']->f('ordertel');
		$begintime=$GLOBALS['le']->f('begintime');
		$endtime=$GLOBALS['le']->f('endtime');
		$tnum=$GLOBALS['le']->f('tnum');
		$member=$GLOBALS['le']->f('member');
		$aid=$GLOBALS['le']->f('aid');
		if (!$aid) return errXML($this->err1073);
		
		if ($aid!=$ac && $member!=$ac) return errXML($this->err24);

		$str="select orderid from uu_order_fx_details where concat_id='$ordern'";
        $GLOBALS['le']->query($str);
		$arr1=array();
		while($row=$GLOBALS['le']->fetch_assoc()) $arr1[]=$row;

		if ($remsg>3) return errXML($this->err16);
		//构造凭证号
		$cert=hashstr((string)$certnum, (string)$salerid);
/*
		$str="select oid,passport,smstype from uu_distributor where id='".$member."'";
        $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$oid=$GLOBALS['le']->f('oid');
		$passport=$GLOBALS['le']->f('passport');
		$smstype=$GLOBALS['le']->f('smstype');

		$str="select oname,fax from uu_org where id='".$oid."'";
        $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$oname=$GLOBALS['le']->f('oname');
		$c_tel=$GLOBALS['le']->f('fax');
*/
 	    require_once("../tel/tel.php");
		require_once("../tel/order1.php");

		if ($num>0) {
		$str="update $table set remsg=remsg+1 where ordernum='$ordern' limit 1";
        $GLOBALS['le']->query($str);
		$a5=array();
		$a5[]=array('done'=>100);
		return toXML($a5);
		}else{
		$str="update $table set smserror='$num' where ordernum='$ordern'";
		$GLOBALS['le']->query($str);
		return errXML($this->err1074);
		}

		}else{
		return errXML($this->err1);
		}

    }

//（票付通）修改、取消订单多功能退款版 参数：订单号，数量（0为取消订单|-1不做修改），修改总额（-1不做修改，票付通内总额跟其他数值一样单独修改），修改取票人手机）
	public function Order_Change_Pro($ac, $pw, $ordern, $num=-1, $totalprice=-1, $ordertel=NULL, $m=NULL) {
		if ($this->authrize($ac, $pw)){
		if (is_numeric($ordern)===false || is_numeric($num)===false || is_numeric($totalprice)===false) return errXML($this->err11);
		if ($num==-1 && $totalprice==-1 && !$valid_begin && !$valid_end && !$ordertel) return errXML($this->err45);
		$table=$this->t7;
		$c_op=$ac;
		//判断是否有修改的权限
		$str="select * from ".$table." where (status=0 or status=2) and ordernum='$ordern' and member='$ac' limit 1";
		$GLOBALS['le']->query($str);
		$r=$GLOBALS['le']->fetch_assoc();
		if (!$r) errXML($this->err24);

		require_once("../conf/pg.le.je");
  		$lee = new pgconn();
		$lee->getConn();
        $pstr="select order_state from order_info where id='".$ordern."'";
		$lee->pgquery($pstr);
		$lee->pgFetchAssoc();
		$order_state=$lee->f('order_state');
		if ($order_state!=0 && $order_state!=4) return errXML($this->err1);9;

		$str="select id from pft_member where status=0 and id=".$r[member];
		$GLOBALS['le']->query($str);
		if (!$GLOBALS['le']->fetch_assoc()) return errXML($this->err1);5;
		
		$totalmoney=$r['totalmoney']?$r['totalmoney']:$r['tprice']*$r['tnum'];
		//构造SQL
		//if ($num!=-1 || $totalprice!=-1) {
			if ($num!=0){
			  //更新订单
			  //totalmoney不相等退出
/* 			  if ($totalmoney!=$totalmoney1) return $this->err44;
			  $totalmoney=$r['tprice']*$num; */
		      $str="update ".$table." set id=id ";
		      if ($num!=-1) $str.=",tnum='$num'";
		      if ($totalprice!=-1) $str.=",totalmoney='$totalprice'";
			  if ($valid_begin) $str.=",begintime='$valid_begin'";
			  if ($valid_end) $str.=",endtime='$valid_end'";
			  if ($ordertel) $str.=",ordertel='$ordertel'";
			  $str.=" where status=0 and ordernum='$ordern' limit 1";
			  $GLOBALS['le']->query($str);
			  
			  $pstr="update order_info set id=id ";
			  if ($num!=-1) $str.=",tnum=$num";
			  if ($totalprice!=-1) $pstr.=",total_price=$totalprice";
			  if ($valid_begin) $pstr.=",begin_time='$valid_begin'";
			  if ($valid_end) $pstr.=",end_time='$valid_end'";
			  if ($ordertel) $str.=",phone_no='$ordertel'";

			  $pstr.=" where id='".$ordern."'";
			  $lee->pgquery($pstr);
			  
			  if ($num!=-1){
				$pstr="update order_wares set quantity=$num where order_id='".$ordern."'";
				$lee->pgquery($pstr);
			  }

 			  $str="update uu_order_addon set dstatus=5,confirm_time=now(),confirm_op='$c_op' where orderid='".$ordern."'";
			  $GLOBALS['le']->query($str); 
			}elseif($num==0){
			  //取消订单
			  $str="update ".$table." set status='3',ctime=now() where ordernum='$ordern' limit 1";
			  if (!$GLOBALS['le']->query($str)) $this->err15;

		      $pstr="update order_info set order_state=2 where id='".$ordern."'";
		      $lee->pgquery($pstr);

 			  //更新订单扩展
			  $str="update uu_order_addon set dstatus=4,confirm_time=now(),confirm_op='$c_op' where orderid='".$ordern."'";
			  $GLOBALS['le']->query($str); 
			}
		//}

		//虚拟账户退款
		if ($flag==0 && $num<$r['tnum']) {
			//分销退款
			$totalprice=($totalprice==-1)?$r['tprice']*$num:$totalprice;
			//改单不收取手续费
			$lm=$totalmoney-$totalprice;
			if ($num==0) {
				//撤单收取手续费
				$str="select reb from uu_jq_ticket where id=".$r['tid'];
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$reb=$GLOBALS['le']->f('reb');
				if ($reb){
					$fee_back=$totalmoney*$reb/100;
					$lm=$lm-$fee_back;
				}
			}
			
			
			if (($r['paymode']==0 || $r['paymode']==1) && $lm>0){
				//分销商+
				$str="update pft_member_money set amoney=amoney+$lm where fid='".$r['member']."' limit 1";
				$GLOBALS['le']->query($str);				
				
				$str="select amoney from pft_member_money where fid='".$r['member']."'";
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$l_money=$GLOBALS['le']->f('amoney');
				$str="insert pft_member_journal set fid='".$r['member']."',dmoney=$lm,lmoney=$l_money,orderid='$ordern',daction=0,rectime=now(),dtype=1,ptype=0";
				$GLOBALS['le']->query($str);
				
				//供应商-
				$str="update pft_member_money set amoney=amoney-$lm where fid='".$r['aid']."' limit 1";
				$GLOBALS['le']->query($str);				
				
				$str="select amoney from pft_member_money where fid='".$r['aid']."'";
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$l_money=$GLOBALS['le']->f('amoney');
				$str="insert pft_member_journal set fid='".$r['aid']."',dmoney=$lm,lmoney=$l_money,orderid='$ordern',daction=1,rectime=now(),dtype=1,ptype=0";
				$GLOBALS['le']->query($str);

				if ($num==0 && $reb){
					//分销商-
					$str="insert pft_member_journal set fid='".$r['member']."',dmoney=$fee_back,lmoney=0,orderid='$ordern',daction=2,rectime=now(),dtype=1,ptype=0";
					$GLOBALS['le']->query($str);
				}
			}elseif($r['paymode']==2 && $lm>0) {
				//处理退信用额度
				//分销商+
				$str="update pft_member_credit set kmoney=kmoney+$lm where fid='".$r['member']."' and aid='".$r['aid']."' limit 1";
				$GLOBALS['le']->query($str);				
				
				$str="select kmoney from pft_member_credit where fid='".$r['member']."' and aid='".$r['aid']."' limit 1";
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$l_money=$GLOBALS['le']->f('kmoney');
				$str="insert pft_member_journal set fid='".$r['member']."',dmoney=$lm,lmoney=$l_money,orderid='$ordern',daction=0,rectime=now(),dtype=1,ptype=2";
				$GLOBALS['le']->query($str);

				if ($num==0 && $reb){
					//分销商-
					$str="insert pft_member_journal set fid='".$r['member']."',dmoney=$fee_back,lmoney=0,orderid='$ordern',daction=2,rectime=now(),dtype=1,ptype=2";
					$GLOBALS['le']->query($str);
				}
			}
		}
		$a5=array();
		$a5[]=array('done'=>100);
		return toXML($a5);
		}else{
		return errXML($this->err1);
		}
    }

//自定义短信发送
	public function Send_SMS_System_Fee($ac, $pw, $tel, $msg, $m=NULL) {
		if ($this->authrize($ac, $pw)){
     	/* if(!preg_match('/^1(3|5|8)\d{9}$/ ',$tel)){
			return $this->err31;
		} */
		$str="select id,dtype from pft_member where account='$ac'";
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$dtype=$GLOBALS['le']->f('dtype');
		$aid=$GLOBALS['le']->f('id');		
		
		if (!$tel || !$msg || !$aid) return errXML($this->err11);
        require_once("../tel/tel.php");
		$_w['sms_pw']="31cfc170f70535b0d4dc92363c486de8";

		$msglen=str_len2($msg);
		$x_ac=($msglen>70)?12:10;
      	$url="http://gateway.woxp.cn:6630/utf8/web_api/?x_eid=10286&x_uid=Denny&x_pwd_md5=".$_w['sms_pw']."&x_ac=".$x_ac."&x_gate_id=300&x_target_no=".$tel."&x_memo=".$msg;
		$num=Get($url);

		if ($num>0) {
			$m=ceil($msglen/67);
			$str="select fee_sms from pft_member where id=$aid limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$fee_sms=$GLOBALS['le']->f('fee_sms');
			$dmoney=$fee_sms*$m;
			
			$str="update pft_member_money set amoney=amoney-$dmoney where fid=$aid";
			$GLOBALS['le']->query($str);		
			
			$str="select amoney from pft_member_money where fid=$aid limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$lmoney=$GLOBALS['le']->f('amoney');
			$str="insert pft_member_journal set fid=$aid,dmoney=$dmoney,lmoney=$lmoney,orderid='$ordern',daction=1,rectime=now(),dtype=8,ptype=0";
			$GLOBALS['le']->query($str);
			
			//平台流水帐
			$str="update pft_member_money set amoney=amoney+$dmoney where fid=1";
			$GLOBALS['le']->query($str);
			$str="select amoney from pft_member_money where fid=1 limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$lmoney=$GLOBALS['le']->f('amoney');		
			$str="insert pft_member_journal set fid=1,dmoney=$dmoney,lmoney=$lmoney,orderid='$ordern',daction=0,rectime=now(),dtype=8,ptype=0";
			$GLOBALS['le']->query($str);			
			
			$a5=array();
			$a5[]=array('done'=>100);
			return toXML($a5);
		}else{
			return errXML($this->err1074);
		}
		}else{
		return errXML($this->err1);
		}
    }

//验证码查看（票付通外部调用）（订单号）
	public function Terminal_Code_Verify($ordern, $m=NULL) {
		if ($this->authrize($ac, $pw)){
		if (is_numeric($ordern)===false) return errXML($this->err11);
		$table=$this->t7;
		
		//权限检查
		
		$str="select certnum,salerid from ".$table." where member='$ac' and ordernum=".$ordern." limit 1";
        $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$certnum=$GLOBALS['le']->f('certnum');
		$salerid=$GLOBALS['le']->f('salerid');
		if (!$certnum || !$salerid) return errXML($this->err24);

		$cert=hashstr((string)$certnum, $salerid);

		//rec

		$str="insert code_check_rec set ordernum='$ordern',account='$ac',dtype=1,dtime=now()";
		$GLOBALS['le']->query($str);

		$a5=array();
	    $a5[]=array("code"=>$cert);
        return toXML($a5);

		}else{
		return errXML($this->err1);
		}

    }


//资金余额查看 (类型0自己的账户余额1可用供应商余额2供应商开放额度，供应商ID（类型>0时必填））
	public function PFT_Member_Fund($dtype=0,$aid=NULL) {
		if ($this->authrize($ac, $pw)){
		$str="select id from pft_member where account='$ac'";
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$id=$GLOBALS['le']->f('id');

		if ($dtype>0 && !$aid) return errXML($this->err3);
		
		$a5=array();
		if ($dtype==0) {
			$str="select amoney from pft_member_money where fid=$id";
			$GLOBALS['le']->query($str);	
			while ($row=$GLOBALS['le']->fetch_assoc()){
				$a5[]=$row;
			}
			return toXML($a5);
		}elseif ($dtype==1){
			$str="select kmoney from pft_member_credit where fid=$id and aid=$aid";
			$GLOBALS['le']->query($str);	
			while ($row=$GLOBALS['le']->fetch_assoc()){
				$a5[]=$row;
			}
			return toXML($a5);		
		}elseif ($dtype==2){
			$str="select basecredit from pft_member_credit where fid=$id and aid=$aid";
			$GLOBALS['le']->query($str);	
			while ($row=$GLOBALS['le']->fetch_assoc()){
				$a5[]=$row;
			}
			return toXML($a5);		
		}
		
		}else{
		return errXML($this->err1);
		}

    }

//会员关系查看（1查看我的分销商2查看我的供应商3查看我的员工）
	public function PFT_Member_Relationship($n, $m=NULL) {
		if ($this->authrize($ac, $pw)){
		$str="select id,dtype from pft_member where account='$ac'";
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$id=$GLOBALS['le']->f('id');
		$dtype=$GLOBALS['le']->f('dtype');
		
		if ($dtype!=0 && $dtype!=1) errXML($this->err24);

		if (!$n) return errXML($this->err3);
		
		$a5=array();
		if ($n==1) {
			if ($dtype==1) return errXML($this->err24);
			$str="select m.dname,m.address,m.qq,m.cname,m.ctel,m.id as mid from pft_member_relationship r left join pft_member m on r.son_id=m.id where r.parent_id=$id and r.son_id_type=0 and r.ship_type=0 and r.status=0 and m.status=0";
			$GLOBALS['le']->query($str);	
			while ($row=$GLOBALS['le']->fetch_assoc()){
				$a5[]=$row;
			}
			return toXML($a5);
		}elseif ($n==2){
			$str="select m.dname,m.address,m.qq,m.cname,m.ctel,m.id as mid from pft_member_relationship r left join pft_member m on r.son_id=m.id where r.son_id=$id and r.son_id_type=0 and r.ship_type=0 and r.status=0 and m.status=0";	
			$GLOBALS['le']->query($str);
			while ($row=$GLOBALS['le']->fetch_assoc()){
				$a5[]=$row;
			}
			return toXML($a5);		
		}elseif ($n==3){
			$str="select m.dname,m.address,m.qq,m.cname,m.ctel,m.id as mid from pft_member_relationship r left join pft_member m on r.son_id=m.id where r.parent_id=$id and r.son_id_type=2 and r.ship_type=0 and r.status=0 and m.status=0";
			$GLOBALS['le']->query($str);	
			while ($row=$GLOBALS['le']->fetch_assoc()){
				$a5[]=$row;
			}
			return toXML($a5);		
		}
		
		}else{
		return errXML($this->err1);
		}

    }	
	


}
?>